<template>
  <div class="personal">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            :class="{ border: form.avatar }"
            action="http://192.168.4.105:7777/e5/file/upload"
            :show-file-list="false"
            :on-change="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <!-- <div v-if="showText" class="avatar-text">
              <span> 更换头像 </span>
            </div> -->
          </el-upload>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.username" disabled />
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="form.nickname" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="所在地">
          <el-select
            v-model="form.prov"
            placeholder="请选择省份"
            @change="getProv"
          >
            <el-option
              v-for="(item, i) in provs"
              :key="i"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <span style="margin: 0 10px; color: #999">-</span>
          <el-select v-model="form.city" placeholder="请选择城市">
            <el-option
              v-for="(item, i) in citys"
              :key="i"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="个性签名">
          <el-input v-model="form.gx" rows="3" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import dataset from './data.js'
export default {
  name: 'Personal',
  data() {
    return {
      form: {
        username: '',
        nickname: '',
        sex: '',
        age: '',
        phone: '',
        gx: '',
        avatar: '',
        prov: '',
        city: ''
      },
      provs: [],
      citys: [],
      showText: true
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  mounted() {
    if (this.form.prov) {
      const result = []
      const { citys } = dataset
      citys.forEach((item) => {
        if (item.prov === this.form.prov) {
          result.push({ value: item.label, label: item.label })
        }
      })
      this.citys = result
    }

    // console.log(getUsers());
  },
  created() {
    this.getUserInfo()
    this.provs = dataset.provs
  },
  methods: {
    async onSubmit() {
      const allAccount = await this.$localStorage.getItem('allAccount')

      const newAccount = allAccount.map((item) => {
        if (item.username === this.form.username) {
          return this.form
        }
        return item
      })
      await this.$localStorage.setItem('allAccount', newAccount)
      this.$store.dispatch('user/getInfo', Object.assign({}, this.form))
      this.$message.success('修改成功')
    },
    handleAvatarSuccess(file) {
      // this.form.avatar = URL.createObjectURL(file.raw)
      const fd = new FileReader()
      fd.readAsDataURL(file.raw)
      // 将文件转化为base64格式写入json文件
      fd.onload = () => {
        const keyString = fd.result
          .toString()
          .replace('data:application/x-pkcs12;base64,', '')
        // console.log("keyString", keyString)
        this.form.avatar = keyString
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    getProv: function(prov) {
      const tempCity = []
      this.citys = []
      this.form.city = ''
      for (var val of dataset.citys) {
        if (prov === val.prov) {
          tempCity.push({ label: val.label, value: val.label })
        }
      }
      this.citys = tempCity
    },
    getCity: function(city) {
      console.log(city)
      console.log(this.selectCity)
    },

    getUserInfo() {
      this.form = Object.assign({}, this.userInfo)
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 30px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader.border ::v-deep .el-upload {
  border-color: transparent;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  border-radius: 30px;
  display: block;
  // object-fit: cover;
}
.avatar-text {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  span {
    color: #fff;
  }
}

.personal {
  display: flex;
  justify-content: center;
  padding: 50px 100px 0;
  .form-box {
    max-width: 700px;
    width: 100%;
  }
}
</style>
